package org.fer.solution;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class WorkerThreadActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.workerthread);

		Button progressButton = (Button) this.findViewById(R.id.progressButton);
		progressButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				new Thread(new Runnable() {

					@Override
					public void run() {
						for (int i = 0; i <= 100; i++) {
							final int j = i;
							try {
								Thread.sleep((int) Math.ceil(Math.random() * 100));

								runOnUiThread(new Runnable() {

									@Override
									public void run() {
										TextView tv = (TextView) findViewById(R.id.textViewPorcentaje);
										tv.setText(j + "%");
									}
								});

							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}).start();
			}
		});
	}
}
